package com.ruoyi.workflow.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableSupport;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.workflow.domain.HistoryTaskResponse;
import com.ruoyi.workflow.domain.TaskTodo;
import com.ruoyi.workflow.domain.TaskTodoResponse;
import com.ruoyi.workflow.service.ITaskTodoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 待办任务Controller
 *
 * @author yuanpingfang
 * @date 2021-04-09
 */
@RestController
@RequestMapping("/workflow/todo")
public class TaskTodoController extends BaseController {
    @Autowired
    private ITaskTodoService taskTodoService;

    /**
     * 查询待办任务列表
     */
    @PreAuthorize("@ss.hasPermi('workflow:todo:list')")
    @GetMapping("/list")
    public TaskTodoResponse list(TaskTodo taskTodo) {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        return taskTodoService.selectTaskTodoList(taskTodo, pageNum, pageSize);
    }

    /**
     * 审核待办任务
     */
    @PreAuthorize("@ss.hasPermi('workflow:todo:completeTask')")
    @Log(title = "审核任务", businessType = BusinessType.COMPLETE)
    @PostMapping(value = "/completeTask")
    public AjaxResult completeTask(@RequestBody TaskTodo taskTodo) {
        return taskTodoService.completeTaskTodo(taskTodo);
    }


    /**
     * 导出待办任务列表
     */
    @PreAuthorize("@ss.hasPermi('workflow:todo:export')")
    @Log(title = " 导出待办任务", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TaskTodo taskTodo) {
        TaskTodoResponse list = taskTodoService.selectTaskTodoList(taskTodo, 1, 99999);
        ExcelUtil<TaskTodo> util = new ExcelUtil<TaskTodo>(TaskTodo.class);
        //return util.exportExcel(list, "todo");
        return null;
    }

    /**
     * 获取待办任务详细信息
     */
    @PreAuthorize("@ss.hasPermi('workflow:todo:query')")
    @GetMapping(value = "/{taskId}")
    public AjaxResult getInfo(@PathVariable("taskId") String taskId) {
        return AjaxResult.success(taskTodoService.selectTaskTodoById(taskId));
    }

    /**
     * 获取任务审批历史
     */
    @PreAuthorize("@ss.hasPermi('workflow:todo:queryHistoryTask')")
    @GetMapping(value = "/queryHistoryTask/{processInstanceId}")
    public HistoryTaskResponse queryHistoryTask(@PathVariable("processInstanceId") String processInstanceId) {
        return taskTodoService.queryHistoryTask(processInstanceId);
    }

    /**
     * 终止流程任务
     */
    @PreAuthorize("@ss.hasPermi('workflow:todo:terminateProcess')")
    @Log(title = "审核任务", businessType = BusinessType.TERMINATE)
    @PostMapping(value = "/terminateProcess")
    public AjaxResult terminateProcess(@RequestBody TaskTodo taskTodo) {
        return taskTodoService.terminateProcess(taskTodo);
    }




}
